package gncom.qr;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class SettingCategory extends Activity {

	private String category_c_id;
	private SQLiteDatabase mDatabase;
	protected CharSequence category_edit_text;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.setting_category);

		Button category_inBtn = (Button) findViewById(R.id.category_inBtn);
		category_inBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				removeDialog(INSERT_CATEGORY);
				showDialog(INSERT_CATEGORY);
			}
		});

		// //// category list setting
		setCategoryList();

	}// end onCreate()

	private void setCategoryList() {
		ArrayList<String> category_list = getCategoryList();
		category_list.remove(0);
		c_idList.remove(0);

		ListView settingList = (ListView) findViewById(R.id.settingList_category);
		settingList.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, category_list));

		settingList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position,
					long arg3) {
				category_edit_text = ((TextView) v).getText();
				category_c_id = c_idList.get(position);
				removeDialog(UP_DEL_CATEGORY);
				showDialog(UP_DEL_CATEGORY);

			}
		});
	}

	private ArrayList<String> c_idList;

	public ArrayList<String> getCategoryList() {
		c_idList = new ArrayList<String>();

		openData();

		ArrayList<String> list = new ArrayList<String>();

		Cursor c = mDatabase.query("categoryNames", null, null, null, null,
				null, "c_id asc");
		startManagingCursor(c);
		c.moveToFirst();
		while (!(c.isAfterLast())) {
			list.add(c.getString(c.getColumnIndex("name")));
			c_idList.add(c.getString(c.getColumnIndex("c_id")));
			c.moveToNext();
		}

		return list;
	}

	private void openData() {
		mDatabase = openOrCreateDatabase("storyInfo",
				SQLiteDatabase.CREATE_IF_NECESSARY, null);
		Cursor c = mDatabase.rawQuery(
				"select name from sqlite_master where type=? and name=?",
				new String[] { "table", "categoryNames" });
		startManagingCursor(c);
		if (c.getCount() == 0) {
			mDatabase
					.execSQL("create table categoryNames(c_id integer primary key autoincrement, name text);");
			ContentValues values = new ContentValues();
			values.put("name", "카테고리추가");
			mDatabase.insert("categoryNames", null, values);
			values.put("name", "기타...");
			mDatabase.insert("categoryNames", null, values);
		}
	}

	private static final int INSERT_CATEGORY = 1;
	private static final int UP_DEL_CATEGORY = 2;

	@Override
	protected Dialog onCreateDialog(int id) {
		LayoutInflater factory = LayoutInflater.from(getApplicationContext());
		final View textEntryView = factory.inflate(R.layout.alert_up_del, null);
		final EditText category_edit = (EditText) textEntryView
				.findViewById(R.id.category_edit);
		switch (id) {
		case INSERT_CATEGORY:

			return new AlertDialog.Builder(this)
					.setIcon(R.drawable.alert_dialog_icon)
					.setTitle("카테고리 생성")
					.setView(textEntryView)
					.setPositiveButton("생성",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									ContentValues values = new ContentValues();
									values.put("name", category_edit.getText()
											.toString());
									mDatabase.insert("categoryNames", null,
											values);
									setCategoryList();
								}
							})
					.setNegativeButton("취소",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {

								}
							}).create();
		case UP_DEL_CATEGORY:
			category_edit.setText(category_edit_text);
			return new AlertDialog.Builder(this)
					.setIcon(R.drawable.alert_dialog_icon)
					.setTitle("카테고리 수정 및 삭제")
					.setView(textEntryView)
					.setPositiveButton("수정",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									ContentValues values = new ContentValues();
									values.put("name", category_edit.getText()
											.toString());
									mDatabase.update("categoryNames", values,
											"c_id=?", new String[] {category_c_id});
									setCategoryList();
								}
							})
					.setNegativeButton("삭제",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									AlertDialog.Builder check = new AlertDialog.Builder(
											SettingCategory.this);
									check.setMessage("카테고리를 삭제하시겠습니까?");
									check.setPositiveButton(
											"확인",
											new DialogInterface.OnClickListener() {
												public void onClick(
														DialogInterface dialog,
														int whichButton) {
													mDatabase.delete("categoryNames",
															"c_id=?", new String[] {category_c_id});
													setCategoryList();
												}
											});
									check.show();
								}
							}).create();
		}

		return null;
	}

}// end class
